var roomGraph = argument0;
var pathGraph = argument1;
var height = array_height_2d(roomGraph);
var width = array_length_2d(roomGraph, 0);

var SEED_SPACING = 20;
var SEED_PROB = .1;
var currentSpacing = 0;
var start = 0;
var finish = 0;
var lastSeed = 0;

var stack = ds_stack_create();

//init start and finish
var startFound = false;
var finishFound = false;
for (var iii = 0; iii < height && (!startFound || !finishFound); iii++)
{
    for (var jjj = 0; jjj < width && (!startFound || !finishFound); jjj++)
    {
        var value = pathGraph[iii, jjj];
        if (is_string(value) && value == 'A')
        {
            start[0] = iii;
            start[1] = jjj;
            startFound = true;
        }
        if (is_string(value) && value == 'B')
        {
            finish[0] = iii;
            finish[1] = jjj;
            finishFound = true;
        }
    }
}

//Seed start and finish
roomGraph[@ start[0], start[1]] = 1;
roomGraph[@ finish[0], finish[1]] = 1;


//Seed rest of path
ds_stack_push(stack, start);

while(!ds_stack_empty(stack))
{
    //get next node
    var current = 0;
    current = ds_stack_pop(stack);
    var currentPathValue = pathGraph[current[0], current[1]];
    
    //handle room seeding
    currentSpacing++;
    if (currentSpacing > SEED_SPACING && irandom(100) / 100 > SEED_PROB)
    {
        roomGraph[@ current[0], current[1]] = 1; //Room seed
        lastSeed = current; 
        currentSpacing = 0;
    }
    
    //TODO: Change this so it deletes the seed before the finish... Look at this later
    //Handle end of path bug (a seed placed too close to finish)
    if (is_string(currentPathValue) && currentPathValue == "B")
    {
        if (currentSpacing < SEED_SPACING)
        {
            roomGraph[@ lastSeed[0], lastSeed[1]] = 0;
        }
    }
    
    //help gamemaker do a comparison against A and real numbers
    if (is_string(currentPathValue) && currentPathValue == 'A')
    {
        currentPathValue = 1;
    }
    //help gamemaker do a comparison against B and real numbers
    if (is_string(currentPathValue) && currentPathValue == 'B')
    {
        currentPathValue = 99999;
    }
    
    //enqueue neighbors
    //east neighbor
    var neighbor0 = 0;
    neighbor0[0] = current[0];
    neighbor0[1] = current[1] + 1;
    var neighbor0Value = -1;
    if (neighbor0[1] < width)
    {
        neighbor0Value = pathGraph[neighbor0[0], neighbor0[1]];
    }  
    
    //north neighbor
    var neighbor1 = 0;
    neighbor1[0] = current[0] - 1;
    neighbor1[1] = current[1];
    var neighbor1Value = -1;
    if (neighbor1[0] > -1)
    {
        neighbor1Value = pathGraph[neighbor1[0], neighbor1[1]];
    }
    
    //west neighbor
    var neighbor2 = 0;
    neighbor2[0] = current[0];
    neighbor2[1] = current[1] - 1;
    var neighbor2Value = -1;
    if (neighbor2[1] > -1) 
    {
        neighbor2Value = pathGraph[neighbor2[0], neighbor2[1]];
    }
    
    //south neighbor
    var neighbor3 = 0;
    neighbor3[0] = current[0] + 1;
    neighbor3[1] = current[1];
    var neighbor3Value = -1;
    if (neighbor3[0] < height)
    {
        neighbor3Value = pathGraph[neighbor3[0], neighbor3[1]];    
    }
    
    if ((is_real(neighbor0Value) && neighbor0Value > currentPathValue) ||
         is_string(neighbor0Value) && neighbor0Value == 'B')
    {
        ds_stack_push(stack, neighbor0);
    }
    if ((is_real(neighbor1Value) && neighbor1Value > currentPathValue) ||
         is_string(neighbor1Value) && neighbor1Value == 'B')
    {
        ds_stack_push(stack, neighbor1);
    }
    if ((is_real(neighbor2Value) && neighbor2Value > currentPathValue) ||
         is_string(neighbor2Value) && neighbor2Value == 'B')
    {
        ds_stack_push(stack, neighbor2);
    }
    if ((is_real(neighbor3Value) && neighbor3Value > currentPathValue) ||
         is_string(neighbor3Value) && neighbor3Value == 'B')
    {
        ds_stack_push(stack, neighbor3);
    }     
}

ds_stack_destroy(stack);